#include <CoOS.h>			                 /*!< CoOS header file            */
#include <stdio.h>
#include "NUC123.h"
#include "fire.h"

#define PLLCON_SETTING  CLK_PLLCON_48MHz_HXT
#define PLL_CLOCK       CFG_CPU_FREQ

/*---------------------------- Symbol Define ---------------------------------*/

#define STACK_SIZE_TASKD 128                 /*!< Define "taskB" task size    */
#define TASK_STK_SIZE    128                 /*!< Define "task_init" task size*/
/*---------------------------- Variable Define -------------------------------*/

OS_STK     taskD_stk[STACK_SIZE_TASKD];	     /*!< Define "taskD" task stack   */
OS_STK     task_init_Stk[TASK_STK_SIZE];	 /*!< Define "task_init" task stack*/

/**
 *******************************************************************************
 * @brief       "taskA" task code
 * @param[in]   None
 * @param[out]  None
 * @retval      None
 * @par Description
 * @details    Complete key detect
 *******************************************************************************
 */

void SYS_Init(void)
{
	/*---------------------------------------------------------------------------------------------------------*/
	/* Init System Clock                                                                                       */
	/*---------------------------------------------------------------------------------------------------------*/

	/* Enable Internal RC 22.1184MHz clock */
	CLK_EnableXtalRC(CLK_PWRCON_OSC22M_EN_Msk);

	/* Waiting for Internal RC clock ready */
	CLK_WaitClockReady(CLK_CLKSTATUS_OSC22M_STB_Msk);

	/* Switch HCLK clock source to Internal RC and HCLK source divide 1 */
	CLK_SetHCLK(CLK_CLKSEL0_HCLK_S_HIRC, CLK_CLKDIV_HCLK(1));

	/* Enable XT1_OUT(PF.0) and XT1_IN(PF.1) */
	SYS->GPF_MFP |= SYS_GPF_MFP_PF0_XT1_OUT | SYS_GPF_MFP_PF1_XT1_IN;

	/* Enable external XTAL 12MHz clock */
	CLK_EnableXtalRC(CLK_PWRCON_XTL12M_EN_Msk);

	/* Waiting for external XTAL clock ready */
	CLK_WaitClockReady(CLK_CLKSTATUS_XTL12M_STB_Msk);

	/* Set core clock as PLL_CLOCK from PLL */
	CLK_SetCoreClock(PLL_CLOCK);

	/* Enable UART module clock */
	CLK_EnableModuleClock(UART0_MODULE);
	CLK_EnableModuleClock(TMR2_MODULE);
	CLK_EnableModuleClock(TMR1_MODULE);
	CLK_EnableModuleClock(PWM01_MODULE);
	CLK_EnableModuleClock(PWM23_MODULE);
	CLK_EnableModuleClock(SPI0_MODULE);

	/* Select UART module clock source */
	CLK_SetModuleClock(UART0_MODULE, CLK_CLKSEL1_UART_S_HXT, CLK_CLKDIV_UART(1));
	CLK_SetModuleClock(TMR2_MODULE, CLK_CLKSEL1_TMR2_S_HXT, 0);
	CLK_SetModuleClock(TMR1_MODULE, CLK_CLKSEL1_TMR2_S_HXT, 0);

	/* Select HCLK as the clock source of SPI0 */
	CLK_SetModuleClock(SPI0_MODULE, CLK_CLKSEL1_SPI0_S_HCLK, MODULE_NoMsk);

	/* Select PWM module clock source */
	CLK_SetModuleClock(PWM01_MODULE, CLK_CLKSEL1_PWM01_S_HXT, 0);
	CLK_SetModuleClock(PWM23_MODULE, CLK_CLKSEL1_PWM23_S_HXT, 0);

	/*---------------------------------------------------------------------------------------------------------*/
	/* Init I/O Multi-function                                                                                 */
	/*---------------------------------------------------------------------------------------------------------*/

	 /* Set PB multi-function pins for UART0 RXD, TXD, TM0, TM1, TM2 and TM2_EXT */
	SYS->GPB_MFP = SYS_GPB_MFP_PB0_UART0_RXD | SYS_GPB_MFP_PB1_UART0_TXD |
	                SYS_GPB_MFP_PB2_TM2_EXT;

	SYS->GPF_MFP = SYS_GPF_MFP_PF2_GPIO | SYS_GPF_MFP_PF3_GPIO;

	SYS->ALT_MFP  = SYS_ALT_MFP_PB2_TM2_EXT;

	 /* Set GPA multi-function pins for PWMA Channel0~3 */
	 SYS->GPA_MFP = SYS_GPA_MFP_PA12_PWM0 | SYS_GPA_MFP_PA13_PWM1 | SYS_GPA_MFP_PA14_PWM2 | SYS_GPA_MFP_PA15_PWM3;

	  /* Setup SPI0 multi-function pins */
	SYS->GPC_MFP = SYS_GPC_MFP_PC0_SPI0_SS0 | SYS_GPC_MFP_PC1_SPI0_CLK | SYS_GPC_MFP_PC2_SPI0_MISO0 | SYS_GPC_MFP_PC3_SPI0_MOSI0;
	SYS->ALT_MFP = SYS_ALT_MFP_PC0_SPI0_SS0 | SYS_ALT_MFP_PC1_SPI0_CLK | SYS_ALT_MFP_PC2_SPI0_MISO0 | SYS_ALT_MFP_PC3_SPI0_MOSI0;
}

void taskD(void* pdata)
{
	int i = 0;
	U32 u32Dis = 0;
	U8 u8T=0, u8D=0;
	//
	while(1)
	{
		DH11_Trigger();
		//DH11_GetEnv(&u8T, &u8D);
		//printf("taskd T %d D %d\n", u8T, u8D);
		//printf("fire %d\n", FIRE_STATE());
		if(i++%2 == 1)
		{
			PB4 = 1;
		}
		else
		{
			PB4 = 0;
		}
		
		CoTickDelay(1000);
	}
}

/**
 *******************************************************************************
 * @brief       Initialization all the task in this application.
 * @param[in]   None
 * @param[out]  None
 * @retval      None
 * @par Description
 * @details    Initialize all the hardware ,Create all the flag and mutex in this application.
 *******************************************************************************
 */
void task_init(void* pdata)
{
	printf("\nsystem hclk %d\n", CLK_GetHCLKFreq());
	//SPIFlash_Init();
	//SR04_Init();
	DH11_Init();
	LED_Init();
	Fire_Init();
	Slc_Init();
	Net_init();
	Shell_Init();	
	CoCreateTask (taskD,0,3,&taskD_stk[STACK_SIZE_TASKD-1],STACK_SIZE_TASKD);
	//IR_Init();
	CoExitTask();
}



int main(void)
	{
	/* Unlock protected registers */
	SYS_UnlockReg();

	/* Init System, IP clock and multi-function I/O */
	SYS_Init();

	/* Lock protected registers */
	SYS_LockReg();

	EInt_Link();

	/* Init UART0 for printf */
	IO_Init();

	GPIO_SetMode(PB, BIT4, GPIO_PMD_OUTPUT);

	  /*!< Initial CooCox CoOS         */
	CoInitOS ();                           
	  
	/*!< Create iniuot tasks	*/
	CoCreateTask(task_init, (void *)0, 10,&task_init_Stk[TASK_STK_SIZE-1], TASK_STK_SIZE);

	CoStartOS ();

	while(1)
	{

	}
}
